﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using HotelSoft.HotelManagement_DTO;
using System.Collections;

namespace HotelSoft.HotelManagement_DAO
{
    public class HoaDon_ThanhToanDAO : AbstractDAO 
    {
        public ArrayList LayDanhSachHoaDon_ThanhToan()
        {
            ArrayList arrDanhSach = new ArrayList();
            string sql = "Select*from HOADON_THANHTOAN";
            DataTable ds = Config.AccessHelper.ExecuteQuery(sql);
            HoaDon_ThanhToanDTO hd;
            int i = 0;
            int iSoDong = ds.Rows.Count;
            int iSoCot = ds.Columns.Count;
            while (i < iSoDong)
            {
                hd = new HoaDon_ThanhToanDTO();
                hd.SoHD = ds.Rows[i]["SoHD"].ToString();
                hd.TongTien = float.Parse(ds.Rows[i]["TongTien"].ToString());
                hd.NgayThanhToan = Convert.ToDateTime(ds.Rows[i]["NgayThanhToan"].ToString());
                hd.SoPDK = ds.Rows[i]["SoPDK"].ToString();
                hd.MasoNV = ds.Rows[i]["MasoNV"].ToString();
                arrDanhSach.Add(hd);
                i++;
            }
            return arrDanhSach;
        }
        public override bool Them(object AbstractDTO)
        {
            try
            {
                HoaDon_ThanhToanDTO dto = (HoaDon_ThanhToanDTO)AbstractDTO;
                string insertCommand = "INSERT INTO HOADON_THANHTOAN VALUES('" +
                   dto.SoHD + "', '" +
                   dto.TongTien + "','" +
                   dto.NgayThanhToan.ToString("MM/dd/yyyy") + "', '" +
                   dto.SoPDK + "', '" +
                   dto.MasoNV + "')";
                Config.AccessHelper.ExecuteQuery(insertCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public override bool CapNhatThongTin(object AbstractDTO)
        {
            return false;
        }
        public static bool CapNhatThongTindondulieu(object AbstractDTO)
        {
            try
            {
                HoaDon_ThanhToanDTO dto = (HoaDon_ThanhToanDTO)AbstractDTO;
                string updateCommand = "UPDATE HOADON_THANHTOAN SET NgayThanhToan = '" + dto.NgayThanhToan.ToString("MM/dd/yyyy") + "', MasoNV = '" + dto.MasoNV + "' WHERE SoHD = '" + dto.SoHD + "'";
                Config.AccessHelper.ExecuteQuery(updateCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public override void TraCuu(object AbstractDTO)
        {
            
        }

        //public static DataTable TraCuudondulieu(object AbstractDTO)
        //{
        //    HoaDon_ThanhToanDTO dto = (HoaDon_ThanhToanDTO)AbstractDTO;
        //    string selectCommand = "SELECT * FROM HOADON_THANHTOAN WHERE SoPDK = '" + dto.SoPDK + "'";
        //    return Config.AccessHelper.ExecuteQuery(selectCommand);
        //}

        public override void Xoa(object AbstractDTO)
        {
            HoaDon_ThanhToanDTO dto = (HoaDon_ThanhToanDTO)AbstractDTO;
            string deleteCommand = "DELETE FROM HOADON_THANHTOAN WHERE SoHD = '" + dto.SoHD + "'";
            Config.AccessHelper.ExecuteNonQuery(deleteCommand);
        }

        internal void Xoa(ArrayList dsHoaDonThanhToan)
        {
            foreach (HoaDon_ThanhToanDTO HD in dsHoaDonThanhToan)
            {
                Xoa(HD);
            }
        }

        public DataTable LayDSHoaDon(object AbstractDTO)
        {
            HoaDon_ThanhToanDTO dto = (HoaDon_ThanhToanDTO)AbstractDTO;
            string strSql = "SELECT SoHD, TongTien, NgayThanhToan, hd.SoPDK, HoTenNV, ChucVu, SDTNV, SoNguoi, NgayDen,SoNgayThue, pdk.SoPhong, kh.CMND, HoTenKH, kh.NgaySinh, kh.GioiTinh, kh.DiaChi, QuocTich, SDTKH, lp.TenLP, gia, tratruoc " +
                            "FROM HOADON_THANHTOAN hd, NHANVIEN nv, KHACHHANG_PHIEUDANGKY kh_pdk, PHIEUDANGKY pdk, KHACHHANG kh, LOAIPHONG lp, PHONG p " +
                            "WHERE nv.MasoNV = hd.MasoNV and hd.SoPDK = pdk.SoPDK and pdk.SoPDK=kh_pdk.SoPDK and kh_pdk.CMND = kh.CMND and p.SoPhong=pdk.SoPhong and p.TenLP=lp.TenLP and SoHD = '" + dto.SoHD + "'";
            return Config.AccessHelper.ExecuteQuery(strSql);
        }

        private HoaDon_ThanhToanDTO HoaDon_ThanhToanDTO(object AbstractDTO)
        {
            throw new NotImplementedException();
        }

        public bool KTHD(object AbstractDTO)
        {
            HoaDon_ThanhToanDTO dto=(HoaDon_ThanhToanDTO)AbstractDTO;
            string strSQL = "Select count(*) From HOADON_THANHTOAN where SoPDK='" + dto.SoPDK + "'";
            return Config.AccessHelper.ExecuteScalar(strSQL);
        }

        public DataTable getInfo(object AbstractDTO)
        {
            HoaDon_ThanhToanDTO dto = (HoaDon_ThanhToanDTO)AbstractDTO;
            string strSQL = "Select NgayThanhToan, Ngayden From HOADON_THANHTOAN hd, PHIEUDANGKY pdk where hd.SoPDK=pdk.SoPDK and hd.SoPDK='" + dto.SoPDK + "'";
            return Config.AccessHelper.ExecuteQuery(strSQL);
        }

        public ArrayList LayDanhSachHoaDon_Tracuu(object AbstractDTO)
        {
            HoaDon_ThanhToanDTO dto = (HoaDon_ThanhToanDTO)AbstractDTO;
            ArrayList arrDanhSach = new ArrayList();
            string sql = "Select * from HOADON_THANHTOAN where SoHD='"+dto.SoHD+"'";
            DataTable ds = Config.AccessHelper.ExecuteQuery(sql);
            HoaDon_ThanhToanDTO hd;
            int i = 0;
            int iSoDong = ds.Rows.Count;
            int iSoCot = ds.Columns.Count;
            while (i < iSoDong)
            {
                hd = new HoaDon_ThanhToanDTO();
                hd.SoHD = ds.Rows[i]["SoHD"].ToString();
                hd.TongTien = float.Parse(ds.Rows[i]["TongTien"].ToString());
                hd.NgayThanhToan = Convert.ToDateTime(ds.Rows[i]["NgayThanhToan"].ToString());
                hd.SoPDK = ds.Rows[i]["SoPDK"].ToString();
                hd.MasoNV = ds.Rows[i]["MasoNV"].ToString();
                arrDanhSach.Add(hd);
                i++;
            }
            return arrDanhSach;
        }

        public bool UpdateTongTien(object AbstractDTO)
        {
            try
            {
                HoaDon_ThanhToanDTO dto = (HoaDon_ThanhToanDTO)AbstractDTO;
                string updateCommand = "UPDATE HOADON_THANHTOAN SET TongTien=" + dto.TongTien + " WHERE SoPDK='" + dto.SoPDK + "'";
                Config.AccessHelper.ExecuteQuery(updateCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
